package com.house.health.controller;

import com.house.health.domain.*;
import com.house.health.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author DuanChangSuo
 * @date 2023/2/7 21:45
 * @description 类的描述和介绍
 **/
@Controller
public class OrderController {
    @Autowired
    private IOrderService iOrderService;

    /**
     * 预定房源
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public String addOrder(String id,HttpServletRequest request) {
        Users u = (Users) request.getSession().getAttribute("loginUser");
        try {
            Order order = new Order();
            order.sethID(Integer.parseInt(id));
            order.setOrderUser(u.getuNickName());
            order.setuID(u.getuID());
            int n = iOrderService.addOrder(order);
            if(n>0) {
                return "OK";
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return "FAIL";
    }

    /**
     * 个人中心点击我的租房信息，跳转页面
     * @return
     */
    @RequestMapping("/myorder")
    public String toMyOrderPage() {
        return "myorder";
    }

    /**
     * 显示该用户预定的房屋信息
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @RequestMapping("/myOrderInfo")
    @ResponseBody
    public UserOrderData findMyOrder(int page, int limit, HttpServletRequest request){
        Page p = new Page();
        p.setPage((page - 1) * limit);
        p.setLimit(limit);
        Users u = (Users) request.getSession().getAttribute("loginUser");
        p.setuID(u.getuID());
        UserOrderData uod = new UserOrderData();
        List<UserOrder> order = iOrderService.findMyOrder(p);
        uod.setCode(0);
        uod.setCount(iOrderService.getOrderCount(u.getuID()));
        uod.setData(order);
        uod.setMsg("200");
        return  uod;
    }

    /**
     * 用户删除租房信息
     * @param oID
     * @return
     */
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public String deleteOrder(int oID) {
        int n = iOrderService.deleteOrder(oID);
        if(n>0)
            return "OK";
        return "FAIL";
    }

    /**
     * 点击房源预约信息，跳转页面
     * @return
     */
    @RequestMapping("/ordermy")
    public String toOrderMyPage() {
        return "ordermy";
    }

    /**
     * 显示自己发布的房子被谁给预定
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @RequestMapping("/orderMyInfo")
    @ResponseBody
    public UserOrderData findOrderMy(int page, int limit, HttpServletRequest request){
        Page p = new Page();
        p.setPage((page - 1) * limit);
        p.setLimit(limit);
        Users u = (Users) request.getSession().getAttribute("loginUser");
        String publisher = u.getuNickName();
        p.setPublisher(publisher);
        UserOrderData uod = new UserOrderData();
        List<UserOrder> order = iOrderService.findOrderMy(p);
        uod.setCode(0);
        uod.setCount(order.size());
        uod.setData(order);
        uod.setMsg("200");
        return  uod;
    }
}